﻿@page "/Admin/Apps"
@attribute [Authorize]
@attribute [TabItemOption(Text ="应用管理",Icon ="fa fa-th")]
@inherits BasePage
@inject ISysAppService SysAppService

<div class="overflow-hidden h-100">
    <Table @ref = "@_table" 
        TItem="SysApp"
        IsPagination="true"
        IsStriped="true"
        IsMultipleSelect="true"
        IsBordered="false"
        IsFixedHeader="true" 
        AllowResizing="true"
        ShowLoading="true"
        ShowColumnList="true"
        ShowCardView="true"
        ShowToolbar="true"
        ShowSearch="true"
        ShowExtendButtons="true" 
        ShowEmpty="true" 
        SearchModel="@_search" 
        SearchMode="SearchMode.Top"
        EditDialogItemsPerRow="1"
        EditDialogSize="Size.Medium"
        ExtendButtonColumnWidth="250"
        PageItemsSource="@(new int[] { 15, 50 })"
        OnQueryAsync="@OnQueryAsync"
        OnSaveAsync="@OnSaveAsync"
        OnDeleteAsync="@OnDeleteAsync">
        <TableColumns>
            <TableColumn @bind-Field="@context.Name" />
            <TableColumn @bind-Field="@context.Code" />                    
            <TableColumn @bind-Field="@context.Icon" Width="80">
                <Template Context="val">
                    <i class="fa @val.Value"></i>
                </Template>
            </TableColumn>
            <TableColumn @bind-Field="@context.Active">
                <Template Context="val">
                    @(val.Value == YesOrNot.Y.ToString() ? "是" : "否")
                </Template>
            </TableColumn>
            <TableColumn @bind-Field="@context.Status" />
            <TableColumn @bind-Field="@context.Sort" />
        </TableColumns>
        <EditTemplate>
            <div class="row g-3 form-inline">
                <div class="col-md-12">
                    <BootstrapInput @bind-Value="@context.Name" maxlength="50" />
                </div>

                <div class="col-md-12">
                    <BootstrapInput @bind-Value="@context.Code" maxlength="50" />
                </div>

                <div class="col-md-12">
                    <IconPicker @bind-Value="@context.Icon" Items="@_icons" PlaceHolder="请点击选择图标" ShowSearch="true"/>
                </div>  

                <div class="col-md-12">
                    <BootstrapInputNumber @bind-Value="@context.Sort" ShowButton="true" Max="999" Min="0" Step="5" />
                </div>
            </div>
        </EditTemplate>
        <RowButtonTemplate>
            @if (context.Active == YesOrNot.N.ToString())
            {
                <PopConfirmButton Size="Size.ExtraSmall" Color="Color.Primary" Text="设为默认" Content="设置为默认应用？" OnConfirm="@(()=>OnConfirm(context))" />
            }
        </RowButtonTemplate>
        <SearchTemplate>
            <GroupBox Title="搜索条件">
                <div class="row g-3 form-inline">
                    <div class="col-3">
                        <BootstrapInput @bind-Value="@context.Name" ShowLabel="true"/>
                    </div>
                    <div class="col-3">
                        <BootstrapInput @bind-Value="@context.Code" ShowLabel="true"/>
                    </div>
                </div>
            </GroupBox>
        </SearchTemplate>
    </Table>
</div>


@code{
    private Table<SysApp>? _table;
    private SysApp _search = new();
    private IEnumerable<SelectedItem>? _icons;

    private Task<QueryData<SysApp>> OnQueryAsync(QueryPageOptions options)
        => SysAppService.PageAsync(_search.Adapt<AppPageInput>(options));

    private Task<bool> OnDeleteAsync(IEnumerable<SysApp> items) 
        => SysAppService.DeleteAsync(items);

    protected override void OnInitialized()
    {
        base.OnInitialized();
        
        _icons = typeof(FontAwesome).ToDisplaySelectList();
    }

    private async Task<bool> OnSaveAsync(SysApp item, ItemChangedType type)
    {
        var result = false;
        switch (type)
        {
            case ItemChangedType.Add:
                result = await SysAppService.AddAsync(item);
                break;
            case ItemChangedType.Update:
                result = await SysAppService.EditAsync(item);
                break;
        }
        return result;
    }

    private async Task OnConfirm(SysApp item)
    {
        await SysAppService.SetAsDefaultAsync(item);
        await _table!.QueryAsync();
    }
}